<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import axios from '@/utils/axios'
// 生成表单实现对象
const ruleFormRef = ref<FormInstance>()
// 接收父组件传参
const props = defineProps({
    id: {
        type: Number,
    },
    username: {
        type: String,
    }
})

// 表单数据
const form = reactive({
    password: '',
    repwd: ''
})

// 自定义密码验证规则
const password = (rules: {}, value: string, callback: Function) => {
    if (!/^\S{3,15}$/.test(value)) {
        callback(new Error('密码格式不正确!!'))
    } else {
        callback()
    }
}
// 自定义密码确认验证规则
const matchPassword = (rules: {}, value: string, callback: Function) => {
    if (value !== form.password) {
        callback(new Error('两次密码输入不一致!!'))
    } else {
        callback()
    }
}
const rules = reactive<FormRules>({
    password: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
        { validator: password, trigger: 'blur' }
    ],
    repwd: [
        { required: true, message: '确认密码不能为空', trigger: 'blur' },
        //  使用自定义验证规则
        { validator: matchPassword, trigger: 'blur' }
    ],
})
// 定义emit方法
const emits = defineEmits(['editSuccess', 'cancle'])

function edit(formEl: FormInstance | undefined) {
    if (!formEl) return
    formEl.validate(async (valid: boolean) => {
        if (valid) {
            // 验证通过
            // 异步提交表单
            const { code } = await axios.put(`/admin/${props.id}`, { password: form.password })
            if (code === 0) {
                // 提示成功
                ElMessage.success('修改成功!')
                // 向父组件传递新增数据,并要求关闭弹层
                emits('editSuccess')
            } else {
                // 提示失败
                ElMessage.error('修改失败!')
            }
        }
    })
}
</script>
<template>
    <el-form :model="form" :rules="rules" ref="ruleFormRef">
        <el-form-item prop="username">
            <el-input v-model="props.username" disabled>
                <template #prefix>
                    <i class="iconfont icon-yonghu1"></i>
                </template>
            </el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input v-model="form.password" placeholder="请修改密码" show-password>
                <template #prefix>
                    <i class="iconfont icon-suoding"></i>
                </template>
            </el-input>
        </el-form-item>
        <el-form-item prop="repwd">
            <el-input v-model="form.repwd" placeholder="请确认密码" show-password>
                <template #prefix>
                    <i class="iconfont icon-suoding"></i>
                </template>
            </el-input>
        </el-form-item>
        <el-form-item class="form-footer">
            <el-button type="primary" @click="edit(ruleFormRef)">确 定</el-button>
            <el-button @click="emits('cancle')">取 消</el-button>
        </el-form-item>
    </el-form>
</template>

<style lang="scss" scoped>
.el-form {
    padding: 30px;

    .el-button {
        margin-top: 30px;
    }
}
</style>